package ui.units {
	import com.greensock.TweenLite;
	import com.greensock.plugins.BezierPlugin;
	import com.greensock.plugins.TweenPlugin;
	
	import flash.display.DisplayObject;
	import flash.geom.Point;
	
	import globals.Global;

	public class Bullet extends BaseUnit {
		
		public var speed:uint;
		
		public var sx:Number;
		public var sy:Number;
		public var ax:Number;
		public var ay:Number
		
		public var start:Point;
		public var end:Point;
		public var target:Monster;
		private var flag:Boolean = true;
//		public var ac
		

		public function Bullet() {
			super();
		}
		
		override public function init(bean:UnitBean):void {
			super.init(bean);
			end = new Point();
			view.x = - view.width;
//			if (bean.url == "Bullet3") {
//				scaleX = scaleY = 2;
//			} else if (bean.url == "Bullet2") {
//				scaleX = scaleY = 0.6;
//			}
			
//			sx = -10;
//			sy = -10;
			ax = 0;
			ay = 1;
		}
		
		override public function update():void {
			if (target == null) {
				return;
			}
			end.x = target.x;
			end.y = target.y;
			super.update();
			
			if (flag) {
				TweenPlugin.activate([BezierPlugin]);
				flag = false;
				speed = 10;
				end.x = target.x;
				end.y = target.y;
				var angle:Number = Math.atan2(end.y - start.y, (end.x - start.x) / 3);
				sx = speed * Math.cos(angle);
				sy = speed * Math.sin(angle) - 10;
				TweenLite.to(this, 0.8, {bezier:[{x:x, y:y}, {x:(x + end.x) / 2, y:y - 100}, {x:end.x, y:end.y}], onComplete:onHit});
			} else {
//				return;
			}
			
//			if (((end.x - x) * (end.x - x) + (end.y - y) * (end.y - y)) <= bean.attackRange * bean.attackRange) {
//				hit();
//			} else {
//				x += sx;
//				y += sy;
//			}
			
//			var angle:Number = Math.atan2(end.y - start.y, end.x - start.x);
//			rotation = angle * 180 / Math.PI;
//			var dx:Number = speed * Math.cos(angle);
//			var dy:Number = speed * Math.sin(angle);
//			if (((end.x - x) * (end.x - x) + (end.y - y) * (end.y - y)) <= bean.attackRange * bean.attackRange) {
//				hit();
//			} else {
//				x += dx;
//				y += dy;
//			}
		}
		
		public function onHit():void {
			hit();
		}
		
		public function hit():void {
			x = end.x;
			y = end.y;
			
			if (target != null) {
				target.hitBlood(bean.damage);
			}
			
			Global.unitManager.removeBullet(id);
		}
	}
}